package com.midea.ai.air.ui.home.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseFragment;
import com.midea.ai.air.common.Constant;
import com.midea.ai.air.ui.device.product.DeviceMainActivity;
import com.midea.ai.air.ui.device.product.data.DeviceInfo;

/**
 * Created by tanchuo on 15/12/9.
 *
 * 空调卡片页面fragment，用于home界面的动态适配
 */
public class AircCardFragment extends BaseFragment
{
    public static final String TAG = "AircCardFragment";
    private View view;
    private View baseView;
    private TextView nameText;
    private TextView homeText;
    private TextView modeText;
    private TextView tempText;
    private TextView tempnotText;

    private ImageView tempDownImage;
    private ImageView tempUpImage;
    private ImageView switchImage;

    private View lineView;
    private int lineWidth;
    private int lineHeight;
    private int score;

    private String name = "",home = "",mode = "";
    private int temp,tempNot;
    private String backColor = "";

    /**
     * 为卡片页面提供数据,用于数据初始化和数据更新,要求数据非空
     *
     * @param name      家电类型
     * @param home      家庭
     * @param mode      运行模式
     * @param temp      温度
     * @param tempNot   温度小数位
     * @param backColor 背景颜色
     */
    public void setData(String name,String home,String mode,int temp,int tempNot,String backColor)
    {
        Log.d("TAG",name+","+home+","+mode+","+temp+","+tempNot);
        if (name == null || home == null || mode == null || backColor == null)
        {
            return;
        }
        this.name = name;
        this.home = home;
        this.mode = mode;
        this.temp = temp;
        this.tempNot = tempNot;
        this.backColor = backColor;

        if (nameText != null)
        {
            nameText.setText(name);
            homeText.setText(home);
            modeText.setText(mode);
            tempText.setText(temp+"");
            tempnotText.setText("." + tempNot);
            view.setBackgroundColor(Color.parseColor(backColor));
        }
    }

    /**
     * LineView 的进度设置
     *
     * @param score 设置的进度百分比
     */
    public void setLineScore(int score)
    {
        this.score = score;
        setLineView();

    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);


        lineWidth = Constant.toPix(Constant.S_DEFAULT_WIDTH - 80);
        lineHeight = Constant.toPixByHeight(4);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.fragment_airc_card,container,false);
        init(view);

        return view;
    }

    private void init(View view)
    {
        nameText = (TextView) view.findViewById(R.id.airc_card_name);
        homeText = (TextView) view.findViewById(R.id.airc_card_home);
        modeText = (TextView) view.findViewById(R.id.airc_card_mode);
        tempText = (TextView) view.findViewById(R.id.airc_card_temp);
        tempnotText = (TextView) view.findViewById(R.id.airc_card_tempnote);
        tempDownImage = (ImageView) view.findViewById(R.id.airc_card_down);
        tempUpImage = (ImageView) view.findViewById(R.id.airc_card_up);
        switchImage = (ImageView) view.findViewById(R.id.airc_card_switch);
        lineView = view.findViewById(R.id.airc_card_line);
        baseView = view.findViewById(R.id.airc_card_baselayout);

        tempText.setTypeface(Constant.sTypefaceNum);
        tempnotText.setTypeface(Constant.sTypefaceNum);

        if (name != null && home != null && mode != null)
        {
            nameText.setText(name);
            homeText.setText(home);
            modeText.setText(mode);
            tempText.setText(temp+"");
            tempnotText.setText("."+tempNot);
        }

        setLineView();

        tempDownImage.setOnClickListener(onClickListener);
        tempUpImage.setOnClickListener(onClickListener);
        switchImage.setOnClickListener(onClickListener);
        baseView.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener = new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.airc_card_baselayout:
                    DeviceMainActivity.startDeviceAct(getActivity(), DeviceInfo.AIRC_TAG);
                    break;
                case R.id.airc_card_down:

                    break;
                case R.id.airc_card_up:

                    break;
                case R.id.airc_card_switch:

                    break;
            }
        }
    };

    private void setLineView()
    {
        if (lineView != null)
        {
            lineView.setLayoutParams(new LinearLayout.LayoutParams(lineWidth * score / 100, lineHeight));
        }
    }

    public void setName(String name)
    {
        if (nameText != null)
        {
            nameText.setText(name);
        }
    }

    public void setHome(String home)
    {
        if (homeText != null)
        {
            homeText.setText(home);
        }
    }

    public void setMode(String mode)
    {
        if (modeText!= null)
        {
            modeText.setText(mode);
        }
    }

    public void setTemp(int temp)
    {
        if (tempText!= null)
        {
            tempText.setText(temp+"");
        }
    }

    public void setTempNot(int tempNot)
    {
        if (tempnotText != null)
        {
            tempnotText.setText(tempNot+"");
        }
    }

    public void setBackgroundColor(String color)
    {
        if (view != null)
        {
            view.setBackgroundColor(Color.parseColor(color));
        }
    }
}
